package com.smartstate.dataArchives.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.smartstate.common.web.exception.CommonException;
import com.smartstate.dataArchives.dto.PageDTO;
import com.smartstate.dataArchives.dto.UserDTO;
import com.smartstate.dataArchives.entity.User;
import com.smartstate.dataArchives.mapper.UserMapper;
import com.smartstate.dataArchives.service.UserService;
import com.smartstate.dataArchives.util.SerialUtil;
import com.smartstate.dataArchives.util.querywapper.WrapperUtil;
import com.smartstate.dataArchives.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Transactional
@Service("TestService")
@Slf4j
public class UserImpl extends ServiceImpl<UserMapper, User> implements UserService {

  @Resource
  SerialUtil serialUtil;
  @Resource
  UserMapper userMapper;
  @Resource
  RedisTemplate redisTemplate;

  @Override
  public List<UserVO> getAllUserAndDepartment(UserDTO item) {
    return userMapper.queryAll(item);
  }

  @Override
  public Page queryAllPageByDTO(PageDTO pageDTO) {
    //创建查询语句包装器和分页查询表对象
    Page<User> page = new Page(pageDTO.getPage(), pageDTO.getSize());
    //根据DTO注解封装查询语句
    Object dto = pageDTO.getDto();
    QueryWrapper<User> wrapper = WrapperUtil.convertQuery(dto, User.class);
    //返回分页查询结果
    return baseMapper.selectPage(page, wrapper);
  }

  @Override
  public String getSerialCode(String name) {
    return serialUtil.getSerialNumber(name);
  }

  @Override
  public Boolean setRedisValue(String value) {
    try {
      redisTemplate.opsForValue().set("test", value);
      return true;
    } catch (Exception ex) {
      throw new CommonException("redis连接异常", 9001);
    }
  }
}
